package com.xz;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author：xz
 * @Date：2023/4/4
 */
public class MethodDemo {
    interface UseString {
        String use(String str, int start, int length);
    }

    private static List<Author> getAuthors() {
        // 初始化数据
        Author author1 = new Author(1L, "梦多", 33, "梦族", null);
        Author author2 = new Author(2L, "呀拉索", 22, "青藏高原", null);
        Author author3 = new Author(3L, "易", 14, "梦族", null);
        Author author4 = new Author(3L, "易", 14, "梦族", null);

        // 书籍列表
        List<Book> books1 = new ArrayList<>();
        List<Book> books2 = new ArrayList<>();
        List<Book> books3 = new ArrayList<>();

//        books1.add(new Book(1L, "刀", "哲学，爱情", 88, "一刀两断"));
//        books1.add(new Book(2L, "一个人刀", "个人成长，爱情", 99, "失败刀"));
//
//        books2.add(new Book(3L, "风", "哲学", 85, "思维"));
//        books2.add(new Book(3L, "风", "哲学", 85, "思维"));
//        books2.add(new Book(4L, "吹不吹", "爱情，个人传记", 56, "哲学家的恋爱观"));
//
//        books3.add(new Book(5L, "剑", "爱情", 56, "武者"));
//        books3.add(new Book(6L, "风剑", "个人传记", 100, "膨胀"));
//        books3.add(new Book(6L, "风剑", "个人传记", 100, "膨胀"));

        author1.setBooks(books1);
        author2.setBooks(books2);
        author3.setBooks(books3);
        author4.setBooks(books3);

        List<Author> authorList = new ArrayList<>(Arrays.asList(author1, author2, author3, author4));
        return authorList;
    }

    private static String subAuthorName(String str, UseString useString) {
        int start = 0;
        int length = 1;
        return useString.use(str,start,length);
    }
    public static void main(String[] args) {
        List<Author> authors = getAuthors();
        // 使用前提
        // 如果我们在重写方法的时候,方法体中只有一行代码,并且且这行代码是调用了第一个参数的成员方法,
        // 并且我们把要重写的抽象方法中，剩余的所有的参数都按照顺序传入了这个成员方法中,这个时候我们就可以引用类的实例方法。
        subAuthorName("三更草堂", new UseString() {
            @Override
            public String use(String str, int start, int length) {
                return str.substring(start, length);
            }
        });
        subAuthorName("三更草堂", String::substring);
    }


}
